package com.hsck.order.kds.base.common


import android.view.ViewGroup
import android.widget.CheckBox
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.base.bean.SelectModel
import com.hsck.order.kds.base.utils.LogUtil

/**
 * 创建者 : ly
 * 时间 : 2017/9/18
 * 公司 : 工博
 * 功能 : 封装基本适配器
 */
abstract class BaseAdapter<T>(@LayoutRes val layoutId: Int) :
    RecyclerView.Adapter<BaseViewHolder>() {

    private var isSingle = false

    //是否为item点击
    private var isItem = false
    private var selectPosition = -1
    private lateinit var recyclerView: RecyclerView

    @IdRes
    private var cbId: Int = -1
    private var list: ArrayList<in SelectModel> = arrayListOf()

    var datas: List<T> = arrayListOf()
        set(value) {
            field = value
            selectPosition = -1
            notifyDataSetChanged()
        }

    var onItemClick: ((T) -> Unit) = { data ->

    }

    var onItemLongClick: ((T) -> Unit) = { data ->
    }

    fun setSingleChoice(recyclerView: RecyclerView, @IdRes idRes: Int, isItem: Boolean = false) {
        this.recyclerView = recyclerView
        this.cbId = idRes
        isSingle = true
        this.isItem = isItem
    }

    fun setMultipleChoice(@IdRes idRes: Int, isItem: Boolean = false) {
        this.cbId = idRes
        isSingle = false
        this.isItem = isItem
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val view = parent.inflate(layoutId)
        return BaseViewHolder(view)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val data = datas[holder.adapterPosition]
        if (cbId != -1) {
            if (isItem) {
                holder.findView<CheckBox>(cbId).isClickable = false
            } else {
                holder.findView<CheckBox>(cbId)
                    .setOnCheckedChangeListener { buttonView, isChecked ->
                        try {
                            if (isSingle) {
                                setSingleChoiceOnClick(holder, holder.adapterPosition)
                            } else {
                                setMultipleChoiceOnClick(holder, holder.adapterPosition)
                            }
                        } catch (e: Exception) {
                            LogUtil.e("error" + e.message)
                        }

                    }
            }
            holder.findView<CheckBox>(cbId).isChecked = (data as SelectModel).isSelected
        }
        holder.itemView.setOnClickListener {
            try {
                if (cbId != -1 && isItem) {
                    if (isSingle) {
                        setSingleChoiceOnClick(holder, holder.adapterPosition)
                    } else {
                        setMultipleChoiceOnClick(holder, holder.adapterPosition)
                    }
                }
                onItemClick(datas[holder.adapterPosition])
            } catch (e: Exception) {
                LogUtil.e("error" + e.message)
            }

        }
        holder.itemView.setOnLongClickListener {
            onItemLongClick(datas[holder.adapterPosition])
            true
        }
        onBindViewHolder(holder, data)
    }

    abstract fun onBindViewHolder(holder: BaseViewHolder, data: T)

    override fun getItemCount(): Int {
        return datas.size
    }

    private fun setSingleChoiceOnClick(nowholder: BaseViewHolder, position: Int) {
        if (selectPosition != -1) {
            //实现单选方法三： RecyclerView另一种定向刷新方法：不会有白光一闪动画 也不会重复onBindVIewHolder
            val holder = recyclerView.findViewHolderForLayoutPosition(selectPosition)
            if (holder != null) {
                //还在屏幕里
                (holder as BaseViewHolder).findView<CheckBox>(cbId).isChecked = false
            } else {
                //add by 2016 11 22 for 一些极端情况，holder被缓存在Recycler的cacheView里，
                //此时拿不到ViewHolder，但是也不会回调onBindViewHolder方法。所以add一个异常处理
                notifyItemChanged(selectPosition)
            }
            //不管在不在屏幕里 都需要改变数据
            (datas[selectPosition] as SelectModel).isSelected = false
        }
        //设置新Item的勾选状态
        selectPosition = position
        (datas[selectPosition] as SelectModel).isSelected = true
        nowholder.findView<CheckBox>(cbId).isChecked = true
    }

    private fun setMultipleChoiceOnClick(holder: BaseViewHolder, position: Int) {
        val cb = holder.findView<CheckBox>(cbId)
        if (isItem) {
            cb.isChecked = !cb.isChecked
        }
        if (cb.isChecked) {
            list.add(datas[position] as SelectModel)
        } else {
            list.remove(datas[position] as SelectModel)
        }
        (datas[position] as SelectModel).isSelected = cb.isChecked
    }

    fun getSelect(): ArrayList<in SelectModel> {
        list.clear()
        if (isSingle) {
            list.add(datas[selectPosition] as SelectModel)
        } else {
            (datas as ArrayList<out SelectModel>).forEach {
                if (it.isSelected) {
                    list.add(it)
                }
            }
        }
        return list
    }

    fun allSelect(isSelect: Boolean) {
        (datas as ArrayList<out SelectModel>).forEach {
            it.isSelected = isSelect
        }
        notifyDataSetChanged()
    }
}